package view;

import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.rmi.RemoteException;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JTextField;

import br.edu.ifpb.calculator.Service;

@SuppressWarnings("serial")
public class GUICalculator extends javax.swing.JFrame implements Action {
	private JButton btDivisao;
    private JButton btMultiplicacao;
    private JButton btSoma;
    private JButton btSubtracao;
    private JTextField txtInput;
    private Service service;
    
    public GUICalculator(Service service) {
    	//instanciar componentes GUI
    	txtInput = new JTextField();
    	btSoma = new JButton(this);
    	btSubtracao = new JButton(this);
    	btMultiplicacao = new JButton(this);
    	btDivisao = new JButton(this);
    	//configurar os componentes GUI
    	//--txtInput
    	txtInput.setBounds(10, 10, 262, 50);
    	txtInput.addKeyListener(new KeyAdapter() {
    		@Override
    		public void keyTyped(KeyEvent e) {
    			//caracteres válidos
    			String chars = "0,123456789";
    			//recuperar o caractere pressionado
    			char c = e.getKeyChar();
    			//localizar caracteres válidos
    	        if(chars.indexOf(c) == -1) {
    	        	e.consume();
    	        }
    			super.keyTyped(e);
    		}
		});
    	getContentPane().add(txtInput);
    	//--btSoma
        btSoma.setText("+");
        getContentPane().add(btSoma);
        btSoma.setBounds(10, 70, 58, 58);
    	btSoma.setActionCommand("sum");
        //--btSubtracao
        btSubtracao.setText("-");
        getContentPane().add(btSubtracao);
        btSubtracao.setBounds(78, 70, 58, 58);
        btSubtracao.setActionCommand("diff");
        //--btMultiplicacao
        btMultiplicacao.setText("*");
        getContentPane().add(btMultiplicacao);
        btMultiplicacao.setBounds(146, 70, 58, 58);
        btMultiplicacao.setActionCommand("mult");
        //--btDivisao
        btDivisao.setText("/");
        getContentPane().add(btDivisao);
        btDivisao.setBounds(214, 70, 58, 58);
        btDivisao.setActionCommand("div");
        //configurar o container
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(290, 200));
        setResizable(false);
        getContentPane().setLayout(null);
        pack();
        //atribuir a instancia do serviço
        this.service = service;
	}
    
    @Override
    public void actionPerformed(ActionEvent e) {
    	//recuperar os dados digitados
    	String[] values = txtInput.getText().split("\\s*,\\s*");
    	//converter em vetor de inteiros
        int[] intValues = new int[values.length];
        for (int ix = 0; ix < values.length; ix++){
            intValues[ix] = Integer.valueOf(values[ix]);
        }
    	//verificar qual o comando enviado e executar
        try {
        	int result = 0;
        	//
	    	if ("sum".equals(e.getActionCommand())){
	    		result = service.sum(intValues);
	    	}
	    	else if ("diff".equals(e.getActionCommand())){
	    		result = service.diff(intValues);
	    	}
	    	else if ("mult".equals(e.getActionCommand())){
	    		result = service.mult(intValues);
	    	}
	    	else if ("div".equals(e.getActionCommand())){
	    		result = service.div(intValues);
	    	}
	    	else {
	    		throw new RuntimeException("Error na implementação das ações");
	    	}
	    	//limpar o input e atualizar com o resultado
            txtInput.setText(String.valueOf(result));
            txtInput.selectAll();
            txtInput.requestFocus();
        }
        catch (RemoteException ex) {
        	throw new RuntimeException("Falha na comunicação: " + ex.getMessage());
		}
    }

	@Override
	public Object getValue(String key) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void putValue(String key, Object value) {
		// TODO Auto-generated method stub
		
	}

}
